package com.ht.api.call.func.impl.notice;

import java.util.ArrayList;
import java.util.List;

import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * 全部消息已读
 * @author asq
 * @createTime 2025年3月9日 09:11:00
 */
public class NoticeAllReadFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "全部消息已读";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		// 生成sql
		List<String> params = new ArrayList<>();
		params.add(me.getLoginIdStr());
		Integer type = param.getInt("type");
		StringBuilder sb = new StringBuilder();
		sb.append("select * from sys_notice where `user`=? and is_read=0");
		if (type != null) {
			sb.append(" and type=?");
			params.add(type + "");
		}
		
		// 执行
		DbList messageList = Db.queryByTable("sys_notice", sb.toString(), params);
		if (!messageList.isEmpty()) {
			messageList.stream().forEach(msg -> {
				msg.set("is_read", 1);
				msg.set("read_time", DateUtil.now());
			});
			Db.insertBatchWithStream(messageList);
		}
		
		return JSONUtil.createObj();
	}

}
